# -*- coding: utf-8 -*-
#*******************************************************************************
#   
#   工具模块
#   整个程序会用到的公共的函数，功能                                  
#                                                                                         
#*******************************************************************************
import os,io,re,time,http
import gzip,base64,socket
import threading,urllib,urllib.request
from html.parser import HTMLParser
from urllib.error import URLError,HTTPError
from datetime import datetime

#---------------------------- 验证模块 ----------------------------

def url_judgement(url):
    return False

#---------------------------- 全局变量 ----------------------------
is_show_request_error=True

#---------------------------- 通用请求 ----------------------------

# 通过给定的地址请求内容
# urlopen 异常不断重试
# read 异常（timeout）不断重试
def request_for(url,my_header=None):
    crequest=None
    if my_header:
        crequest=urllib.request.Request(url=url,headers=my_header)
    else:
        crequest=urllib.request.Request(url=url)
    # 出现异常，重复申请
    while True:
        try:
            # 通过request获取response
            cresponse=urllib.request.urlopen(crequest,timeout=20)
            # 读取内容
            content=cresponse.read()
            # 出现异常，睡眠一会再尝试 URLError,HTTPError,read函数异常
        except HTTPError as err:
            # 处理HTTP异常
            if err.code==404:   return False        # 没救了，别循环了
        except URLError as err:
            # 网络异常
            time.sleep(3)
            continue
        except Exception as err:
            # 读取异常
            if is_show_request_error:
                print(url,err)
            cresponse.close()
            time.sleep(3)
            continue
        else:
            encoding=cresponse.getheader('content-encoding')
            if encoding:
                if 'gzip' in encoding: content=gzip.decompress(content)
            cresponse.close()
            return content

# 返回一个字符串 title{year,month,day,hour,minute,second}
def title_by_time():
    time=datetime.now().timetuple()
    attrs=[]
    for v in time:
        attrs.append(str(v))
    return '_'.join(attrs)

# 将10进制转化为(2,36)进制任意一种
def toRadix(val,base=10):
    try:
        val=int(val)
        base=int(base)
    except Exception as err:
        print(err)
        return '-1'
    digits=[]
    if val==0: return '0'
    while val>0:
        m=val%base
        val=int(val/base)
        if 0<=m<=9:
            digits.append(chr(m-0+ord('0')))
        else:
            digits.append(chr(m-10+ord('a')))
    digits.reverse()
    return ''.join(digits)

# 返回一个前导0的字符串
def zero_prefix(val,lens=4):
    val=int(val)
    digits=[]
    for i in range(0,lens):
        digits.append(str(val%10))
        val=int(val/10)
    digits.reverse()
    return ''.join(digits)

# 收集属性信息，返回一个信息字典
def info_collect(attrs):
    infos={}
    for (v,k) in attrs:
        infos[v]=k
    return infos

# 一般的请求头部
common_headers={
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Encoding':'gzip, deflate, sdch',
    'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4',
    'Connection':'keep-alive',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'
}